package com.logistic.server.controller;

import com.logistic.server.common.annotation.LoginRequired;
import com.logistic.server.entity.common.PageRequest;
import com.logistic.server.entity.common.Result;
import com.logistic.server.entity.common.ResultGenerator;
import com.logistic.server.entity.pojo.SysDictType;
import com.logistic.server.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 字典类型 前端控制器
 * </p>
 *
 * @author swen
 * @since 2021-10-25
 */
@Api(value = "字典类型接口", tags = {"字典类型接口"})
@Slf4j
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController {

    @Resource
    private ISysDictTypeService dictTypeService;

    /**
    * 字典类型列表
    * @param dictType 字典类型
    * @param pageRequest 分页参数
    * @return Result
    */
    @ApiOperation(value = "字典类型列表", notes = "字典类型列表")
    @GetMapping
    public Result<?> list(SysDictType dictType, PageRequest pageRequest) {
        return ResultGenerator.genSuccessResult(dictTypeService.selectPageDictTypeList(dictType, pageRequest));
     }

    /**
    * 字典类型新增
    * @param dictType 字典类型
    * @return Result
    */
    @ApiOperation(value = "字典类型新增", notes = "字典类型新增")
    @PostMapping
    @LoginRequired
    public Result<?> add(@RequestBody SysDictType dictType) {
        dictTypeService.insertDictType(dictType);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 字典类型删除
    * @param ids 字典类型主键
    * @return Result
    */
    @ApiOperation(value = "字典类型删除", notes = "字典类型删除")
    @DeleteMapping("/{ids}")
    @LoginRequired
    public Result<?> delete(@PathVariable Long[] ids) {
        dictTypeService.deleteDictTypeByIds(ids);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 字典类型修改
    * @param dictType 字典类型
    * @return Result
    */
    @ApiOperation(value = "字典类型修改", notes = "字典类型修改")
    @PutMapping
    @LoginRequired
    public Result<?> update(@RequestBody SysDictType dictType) {
        dictTypeService.updateDictType(dictType);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 字典类型详情
    * @param id 字典类型主键
    * @return Result
    */
    @ApiOperation(value = "字典类型详情", notes = "字典类型详情")
    @GetMapping("/{id}")
    public Result<?> detail(@PathVariable Long id) {
        return ResultGenerator.genSuccessResult(dictTypeService.selectDictTypeById(id));
    }

    /**
     * 刷新字典缓存
     */
    @ApiOperation("刷新字典缓存")
    @DeleteMapping("/refreshCache")
    public Result<?> refreshCache() {
        dictTypeService.resetDictCache();
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 获取字典选择框列表
     */
    @ApiOperation("获取字典选择框列表")
    @GetMapping("/optionSelect")
    public Result<List<SysDictType>> optionSelect() {
        return ResultGenerator.genSuccessResult(dictTypeService.selectDictTypeAll());
    }
}
